package com.zbkj.crmeb.nsl.nslwxapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.CommonResult;
import com.zbkj.crmeb.nsl.nslemons.model.NslCompany;
import com.zbkj.crmeb.nsl.nslemons.service.NslCompanyService;
import com.zbkj.crmeb.nsl.nslwxapp.model.NslCollection;
import com.zbkj.crmeb.nsl.nslwxapp.response.*;
import com.zbkj.crmeb.nsl.nslwxapp.service.EbSystemGroupDataService;
import com.zbkj.crmeb.nsl.nslwxapp.service.NslCollectionService;
import com.zbkj.crmeb.nsl.nslwxapp.service.NslCraneService;
import com.zbkj.crmeb.system.service.SystemGroupDataService;
import com.zbkj.crmeb.user.model.User;
import com.zbkj.crmeb.user.model.UserLevel;
import com.zbkj.crmeb.user.service.UserLevelService;
import com.zbkj.crmeb.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 组合数据详情表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-01
 */

@RestController
@RequestMapping("api/admin/nsl")
@Api(tags = "nsl-首页")
public class EbSystemGroupDataController {

    @Autowired
    private EbSystemGroupDataService ebSystemGroupDataService;

    @Autowired
    private NslCraneService nslCraneService;
    @Autowired
    private NslCollectionService collectionService;
    @Autowired
    private UserService userService;
    @Autowired
    private SystemGroupDataService systemGroupDataService;
    @Autowired
    private NslCompanyService nslCompanyService;
    @Autowired
    private UserLevelService userLevelService;

    @ApiOperation(value = "首页导航图")
    @GetMapping("/index")
    public CommonResult getAll(){

        /**
         * 获取首页banner图
         */
        List banner = ebSystemGroupDataService.PublishCourseInfo();

        /**
         * 获取首页Icon图
         */
        List icon = ebSystemGroupDataService.getIcon();

        /**
         * 获取最新车辆
         */
        List<CannerCbrandsEntry> hotCraneList = nslCraneService.getHotCraneList();

        /**
         * 获取最火车辆
         */
        List<CannerCbrandsEntry> newCraneList = nslCraneService.getNewCraneList();

        Map map = new HashMap();
        map.put("Banner",banner);
        map.put("icon",icon);
        map.put("hotcrane",hotCraneList);
        map.put("newcrane",newCraneList);
        return CommonResult.success(map);
    }

    /**
     * 我的
     */
    @PostMapping("/user/index/{userId}")
    @ApiOperation(value = "我的")
    public CommonResult info(@PathVariable  int userId){

        User userInfo = userService.getById(userId);
//        UserInfo userInfo = userService.getUserInfo(userId);

        List listByGid = systemGroupDataService.getSystemByGidId(54);
        List listByGids = systemGroupDataService.getSystemByGidId(74);

        Map map = new HashMap();
        map.put("userinfo",userInfo);
        map.put("iconserver",listByGid);
        map.put("iconsystem",listByGids);

        return CommonResult.success(map);
    }

    /**
     * 会员到期时间及天数
     */
    @PostMapping("/user/getVipInfo")
    @ApiOperation(value = "会员到期时间及天数")
    public CommonResult getVipInfo(@RequestParam Integer userId,@RequestParam String isFlag){
        UserLevel userLevel = userLevelService.getUserLevelByUserId(userId);
        RepUserInfo repUserInfo = null;
        Date currentDate = new Date();
        if (userLevel == null){
            if (isFlag.equals("getAdd")){
                userLevelService.insertUserLevel(userId,new Date(currentDate.getTime()+3*24*60*60*1000),currentDate);
            }
            repUserInfo = userService.getRepUserInfo(userId);
            repUserInfo.setIsNewUser("1");
            repUserInfo.setTipMsg("您是当前系统新用户，免费享受3天VIP会员特权!");
        } else {
            repUserInfo = userService.getRepUserInfo(userId);
            repUserInfo.setIsNewUser("0");
            repUserInfo.setTipMsg("");
        }

        //查询绑定表判断用户是否创建公司，并返回标识  1创建，0未创建
        Integer count = nslCompanyService.selectISCreateCom(userId);
        if(count>0){
            NslCompany nslCompany = nslCompanyService.selectcompanyByUserId(userId);
            repUserInfo.setIsCreateCom(nslCompany.toString());
        }else {
            repUserInfo.setIsCreateCom("0");
        }

        return CommonResult.success(repUserInfo);
    }

    /**
     * 获取收藏列表
     */
    @PostMapping("/collectlist/{userId}")
    @ApiOperation(value = "获取收藏列表")
    public CommonResult getAllConnection(@PathVariable Integer userId){

        List<CollectAndCraneList> lists = nslCraneService.getcollectAndCraneList(userId);
        return CommonResult.success(lists);
    }

    /**
     * 车辆收藏
     */
    @PostMapping("addCollection/{userid}/{craneid}")
    @ApiOperation(value = "车辆收藏")
    public CommonResult addCollection(@PathVariable Integer userid,
                                      @PathVariable Integer craneid){

        NslCollection collection = new NslCollection();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userid",userid);
        int count = collectionService.count(queryWrapper);
        if(count > 10){
            return CommonResult.success("每个用户只能收藏10条数据!");
        }
        collection.setCraneid(craneid);
        collection.setUserid(userid);
        collection.setType("0");
        collection.setKbn("0");
        boolean save = collectionService.save(collection);
        return CommonResult.success(save);
    }

    /**
     * 取消车辆收藏
     */
    @PostMapping("cancelCollection/{userid}/{craneid}")
    @ApiOperation(value = "取消车辆收藏")
    public CommonResult deleteCollection(@PathVariable Integer userid,
                                      @PathVariable Integer craneid){

        int count = collectionService.cancelCollection(userid,craneid);
        if (count>0){
            return CommonResult.success("取消成功");
        }
        return CommonResult.failed("取消失败");

    }
}

